import { Body, Controller, Param, Post, Req } from '@nestjs/common';
import { errorReturn, successReturn } from 'src/util';
import { LoginService } from './login.service';

@Controller('login')
export class LoginController {
    constructor(
        private loginService:LoginService
    ) {}

    @Post()
    async login(@Body() params:{code:string}, @Req() { clientSource }) {
        if(!params.code) return 'it is error'
        try {
            let result = await this.loginService.login(params.code)
            if(result) {
                return successReturn(result)
            }
        } catch (error) {
            
        }
        
        
        return errorReturn()
    }

    @Post('refresh')
    async refresh(@Body() params : {token:string}, @Req() { user }) {
        const res = await this.loginService.refresh(user.userId, params.token)
        return res ? successReturn(res) : errorReturn()
    }
}
